home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / startrek / trek73 / src / parsit.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  3KB  |  125 lines

  1. /*
  2.  * TREK73: parsit.c
  3.  *
  4.  * Parse and get input
  5.  *
  6.  * Gets, parsit (courtesy, P. Lapsley)
  7.  *
  8.  */
  9. #include <stdio.h>
  10. static int gindex;
  11. static char **argv;
  12. char *
  13. Gets(buf)
  14. char *buf;
  15. {
  16.     extern char *gets();
  17.  
  18.     if (argv[gindex] == NULL)
  19.         return (gets (buf));
  20.     ++gindex;
  21.     if (argv[gindex] == NULL)
  22.         return (gets (buf));
  23.     strcpy (buf, argv[gindex]);
  24.     puts (buf);
  25.     return (buf);
  26. }
  27.  
  28. /*
  29. ** parsit.c  23 September 1984  P. Lapsley (phil@Berkeley.ARPA)
  30. **
  31. ** Parse a string of words separated by spaces into an
  32. ** array of pointers to characters, just like good ol' argv[]
  33. ** and argc.
  34. **
  35. ** Usage:
  36. **
  37. ** char line[132];
  38. ** char **argv;
  39. ** int argc;
  40. **
  41. **    argv = (char **) NULL;
  42. **    argc = parsit(line, &argv);
  43. **
  44. ** returns the number of words parsed in argc.  argv[argc] will
  45. ** be (char *) NULL to indicate end of list, if you're not
  46. ** happy with just knowing how many words you have.
  47. **
  48. ** Note that setting argv = (char **) NULL is only done the first
  49. ** time the routine is called with a new "argv" -- it tells
  50. ** parsit that "argv" is a new array, and parsit shouldn't free
  51. ** up the elements (as it would do if it were an old array).
  52. */
  53.  
  54.  
  55. parsit(line, array)
  56. char *line;
  57. char ***array;
  58. {
  59.     char *malloc();
  60.     char word[132];
  61.     char *linecp;
  62.     int i, j, num_words;
  63.  
  64.     gindex = 0;
  65.     argv = *array;
  66.     if (argv != (char **) NULL) {  /* Check to see if we should */
  67.         i = 0;               /* free up the old array */
  68.         do {
  69.             free(argv[i]);    /* If so, free each member */
  70.         } while (argv[i++] != (char *) NULL);
  71.         free(argv);        /* and then free the ptr itself */
  72.     }
  73.  
  74.     linecp = line;
  75.     num_words = 0;
  76.     while (1) {    /* count words in input */
  77.         for (; *linecp == ' ' || *linecp == '\t'; ++linecp)
  78.             ;
  79.         if (*linecp == '\0')
  80.             break;
  81.  
  82.         for (; *linecp != ' ' && *linecp != '\t' && *linecp != '\0'; ++linecp)
  83.             ;
  84.         ++num_words;
  85.         if (*linecp == '\0')
  86.             break;
  87.     }
  88.  
  89.     /* Then malloc enough for that many words plus 1 (for null) */
  90.  
  91.     if ((argv = (char **) malloc((num_words + 1) * sizeof(char *))) ==
  92.         (char **) NULL) {
  93.         fprintf(stderr, "parsit: malloc out of space!\n");
  94.         return(0);
  95.     }
  96.  
  97.     j = i = 0;
  98.     while (1) {    /* Now build the list of words */
  99.         for (; *line == ' ' || *line == '\t'; ++line)
  100.             ;
  101.         if (*line == '\0')
  102.             break;
  103.  
  104.         i = 0;
  105.         for (; *line != ' ' && *line != '\t' && *line != '\0'; ++line)
  106.             word[i++] =  *line;
  107.         word[i] = '\0';
  108.         argv[j] = malloc(strlen(word) + 1);
  109.         if (argv[j] == (char *) NULL) {
  110.             fprintf(stderr, "parsit: malloc out of space!\n");
  111.             return(0);
  112.         }
  113.  
  114.         strcpy(argv[j], word);
  115.         ++j;
  116.         if (*line == '\0')
  117.             break;
  118.     }
  119.     argv[j] = (char *) NULL;  /* remember null at end of list */
  120.     *array = argv;
  121.     return(j);
  122. }
  123.  
  124.  
  125.